<?php

namespace App\Http\Controllers;

use App\Contract\OrderStatus;
use App\Contract\OrderType;
use App\Http\Service\AdaPayService;
use App\Http\Service\DeviceService;
use App\Model\AgentAmountLog;
use App\Model\AgentCashLog;
use App\Model\Bill;
use App\Model\BillPay;
use App\Model\DeviceTask;
use App\Model\Message;
use App\Model\Order;
use App\Model\UserAmountLog;
use App\Model\UserCashLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AgentPaymentNotifyController
{
    public function notify(Request $request)
    {
        Log::info('payment notify', $request->all());
        $data = $request->input('data');
        $sign = $request->input('sign');
        $type = $request->input('type');
        $payment = AdaPayService::getPayment();
        if (!$payment->ada_tools->verifySign($sign, $data)) {
            Log::error('支付回调验签失败', [$request->all()]);
            return 'fail';
        }
        switch ($type) {
            case 'payment.succeeded':
                // 支付成功
                $data = json_decode($data);
                break;
            case 'payment.failed':
                // 支付失败
                break;
            case 'refund.succeeded':
                // 退款成功
                break;
            case 'refund.failed':
                // 退款失败
            case 'corp_member.succeeded':
                // 开户成功
                break;
            case 'fast_card.succeeded':
                // 快捷绑卡确认成功
                break;
            case 'fast_card.failed':
                // 快捷绑卡确认失败
                break;
            case 'cash.failed':
                // 取现失败
                $this->drawCashStatus(json_decode($data), 'F');
                break;
            case 'cash.succeeded':
                // 取现成功
                $this->drawCashStatus(json_decode($data), 'S');
                break;
        }
        return 'success';
    }


    public function drawCashStatus($data, $status)
    {
        $cashLog = AgentCashLog::query()->where('order_no', $data->order_no)->with(['agent'])->first();
        $cashLog->status = $status;
        $cashLog->save();
        if ($status == 'F') {
            $cashLog->refund_no =  "TXRF_". date("YmdHis").rand(10000, 99999);
            $params = array(
                'app_id'=>  AdaPayService::getAppId(),
                'balance_refund_no'=>$cashLog->refund_no,
                'balance_refund_amt'=>$cashLog->amount,
                'balance_seq_id'=> $cashLog->pay_no,
                'reason' => '提现失败余额退款'.$cashLog->pay_no
            );
            $settleAccount = AdaPayService::getSettleAccount();
            $settleAccount->balanceRefund($params);
            if($settleAccount->isError()){
                $cashLog->status = 'T';
            }else{
                $cashLog->status = $status;
            }
            $cashLog->save();
            // 扣余额
            $cashLog->agent->amount += $cashLog->cash_amt;
            $cashLog->agent->save();
            // 写入账户明细
            AgentAmountLog::create([
                'agent_id' => $cashLog->agent->id,
                'type' => 6,
                'amount' => $cashLog->cash_amt,
                'log' => '提现失败退回'
            ]);
            Message::create([
                'user_id'=> $cashLog->agent_id,
                'title' => '提现失败通知',
                'content' => '提现金额：￥' . $cashLog->cash_amt
            ]);
        } else {
            Message::create([
                'user_id'=> $cashLog->agent_id,
                'title' => '提现成功通知',
                'content' => '提现金额：￥' . $cashLog->cash_amt
            ]);
        }
    }
}
